﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace atmsystem_ss
{
    public partial class frm_Login_Account_Customer : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dtAccount = new DataTable("tblAccount");
        private DataTable dtCustomer = new DataTable("tblCustomer");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        
        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        private bool IsNumber(String str)
        {
            foreach (Char c in str)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
        private bool tontai()
        {
            dtAccount.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblAccount where fldID_Account LIKE '%'+@fldID_Account+'%' ";
            command.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
            da.SelectCommand = command;
            da.Fill(dtAccount);            
            if (dtAccount.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        private bool tontai_Account()
        {
            dtAccount.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblAccount where (fldID_Account LIKE '%'+@fldID_Account+'%') and (fldPIN like '%'+@fldPIN+'%') ";
            command.Parameters.Add("@fldID_Account", SqlDbType.NVarChar, 50).Value = txtID_Account.Text;
            command.Parameters.Add("@fldPIN", SqlDbType.Char, 4).Value = txtPassword_Account.Text;
            da.SelectCommand = command;
            da.Fill(dtAccount);
            if (dtAccount.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        private bool kiemtra_PIN() {
            kt = true;
            if (txtPassword_Account.Text == "")
            {
                txtPassword_Account.Focus();
                MessageBox.Show("Ban can nhap PIN", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            
            else if (IsNumber(Convert.ToString(txtPassword_Account.Text)) == false)
            {
                txtPassword_Account.Focus();
                MessageBox.Show("PIN la mot chuoi so nguyen, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (Convert.ToString(txtPassword_Account.Text).Length.Equals(4) == false)
            {
                txtPassword_Account.Focus();
                MessageBox.Show("Chu y PIN gom 4 so, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (tontai_Account() == true)
            {
                txtPassword_Account.Text = "";
                txtPassword_Account.Focus();
                MessageBox.Show("PIN sai", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }

            return kt;
        }
        private bool kiemtra_ID_Account()
        {
            kt = true;
            if (txtID_Account.Text == "")
            {
                txtID_Account.Focus();
                MessageBox.Show("Ban can nhap Account ID", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (IsNumber(Convert.ToString(txtID_Account.Text)) == false)
            {
                txtID_Account.Focus();
                MessageBox.Show("Account ID la mot chuoi so nguyen, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (Convert.ToString(txtID_Account.Text).Length.Equals(12) == false)
            {
                txtID_Account.Focus();
                MessageBox.Show("Chu y Account ID gom 12 so, moi ban nhap lai !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (tontai() == true)
            {
                txtID_Account.Text = "";
                txtID_Account.Focus();
                MessageBox.Show("Account ID khong ton tai, ban vui long nhap Account ID khac !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }

            return kt;
        }

        public frm_Login_Account_Customer()
        {
            InitializeComponent();
        }

        private void frm_Login_Account_Customer_Load(object sender, EventArgs e)
        {
            connect();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            kiemtra_ID_Account();
            if (kt == true)
            {
                dtCustomer.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select distinct fldFull_Name from tblCustomer join tblAccount on tblCustomer.fldID_Customer = tblAccount.fldID_Customer where fldID_Account LIKE '%'+@fldID_Account+'%' ";
            command.Parameters.Add("@fldID_Account", SqlDbType.VarChar, 50).Value = txtID_Account.Text;
            da.SelectCommand = command;
            da.Fill(dtCustomer);
            label1.Visible = true;
                label1.DataBindings.Add("Text", dtCustomer, "fldFull_Name", true);

                labEnterID_Account.Visible =false;
                labEnterPIN.Visible = true;
                btnNext.Visible = false;
                btnLogin.Visible = true;
                btnCancel.Visible = true;
                txtID_Account.Visible = false;
                txtPassword_Account.Visible = true;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            kiemtra_PIN();
            if (kt == true)
            {

                frmCustomer_Account _frmCustomer_Account = new frmCustomer_Account();
                _frmCustomer_Account.strID_Account_FCA = txtID_Account.Text.ToString();
                _frmCustomer_Account.Show();
                Hide();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtID_Account.Clear();
            label1.Visible = false;
            labEnterID_Account.Visible = true;
            labEnterPIN.Visible = false;
            btnNext.Visible = true;
            btnLogin.Visible = false;
            btnCancel.Visible = false;
            txtID_Account.Visible = true;
            txtPassword_Account.Visible = false;
        }

        private void btnAdmin_Click(object sender, EventArgs e)
        {
        
            frmLogin_Admin _frmLogin_Admin = new frmLogin_Admin();
            _frmLogin_Admin.Show();
            
        }

     
    }
}
